<template>
  <div class="dashboard-container">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="asideWidth" class="sidebar">
        <div class="logo-section">
          <h3 v-if="!isCollapse" class="logo-text">商家管理系统</h3>
          <div v-else class="mini-logo">商</div>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          :collapse="isCollapse"
          :unique-opened="true"
          class="sidebar-menu"
          router
        >
          <el-menu-item index="/dashboard">
            <el-icon><House /></el-icon>
            <template #title>首页</template>
          </el-menu-item>
          
          <el-menu-item index="/dashboard/orders">
            <el-icon><Document /></el-icon>
            <template #title>订单管理</template>
          </el-menu-item>
          
          <el-sub-menu index="staff">
            <template #title>
              <el-icon><User /></el-icon>
              <span>人员管理</span>
            </template>
            <el-menu-item index="/dashboard/users">
              <el-icon><Avatar /></el-icon>
              <template #title>用户管理</template>
            </el-menu-item>
            <el-menu-item index="/dashboard/roles">
              <el-icon><UserFilled /></el-icon>
              <template #title>角色管理</template>
            </el-menu-item>
          </el-sub-menu>
          
          <el-sub-menu index="food">
            <template #title>
              <el-icon><Food /></el-icon>
              <span>美食管理</span>
            </template>
            <el-menu-item index="/dashboard/categories">
              <el-icon><Menu /></el-icon>
              <template #title>分类管理</template>
            </el-menu-item>
            <el-menu-item index="/dashboard/dishes">
              <el-icon><Bowl /></el-icon>
              <template #title>菜品管理</template>
            </el-menu-item>
          </el-sub-menu>
          
          <el-menu-item index="/dashboard/revenue-analysis">
            <el-icon><TrendCharts /></el-icon>
            <template #title>收益分析</template>
          </el-menu-item>
          
          <el-menu-item index="/dashboard/members">
            <el-icon><Members /></el-icon>
            <template #title>会员管理</template>
          </el-menu-item>
          
          <el-sub-menu index="settings">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>商家设置</span>
            </template>
            <el-menu-item index="/dashboard/store-name">
              <el-icon><Shop /></el-icon>
              <template #title>本店名称</template>
            </el-menu-item>
            <el-menu-item index="/dashboard/wifi-settings">
              <el-icon><WifiIcon /></el-icon>
              <template #title>店内WiFi</template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="header">
          <div class="header-left">
            <el-icon class="collapse-btn" @click="toggleCollapse">
              <Expand v-if="isCollapse" />
              <Fold v-else />
            </el-icon>
            <div class="breadcrumb">
              <span class="current-page">{{ currentPageTitle }}</span>
            </div>
          </div>
          
          <div class="header-right">
            <el-dropdown>
              <div class="user-info">
                <el-icon class="user-avatar"><Avatar /></el-icon>
                <span class="username">{{ username }}</span>
                <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="logout">
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 内容区域 -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import {
  House,
  Document,
  User,
  Food,
  Setting,
  Expand,
  Fold,
  Avatar,
  ArrowDown,
  SwitchButton,
  UserFilled,
  Menu,
  Bowl,
  Shop,
  TrendCharts,
  UserFilled as Members
} from '@element-plus/icons-vue'
import WifiIcon from '@/components/icons/WifiIcon.vue'

const router = useRouter()
const route = useRoute()

const isCollapse = ref(false)
const username = ref('张三')

const asideWidth = computed(() => isCollapse.value ? '64px' : '220px')

const activeMenu = computed(() => {
  return route.path
})

const currentPageTitle = computed(() => {
  const titleMap: Record<string, string> = {
    '/dashboard': '首页',
    '/dashboard/orders': '订单管理',
    '/dashboard/users': '用户管理',
    '/dashboard/roles': '角色管理',
    '/dashboard/categories': '分类管理',
    '/dashboard/dishes': '菜品管理',
    '/dashboard/revenue-analysis': '收益分析',
    '/dashboard/members': '会员管理',
    '/dashboard/store-name': '本店名称',
    '/dashboard/wifi-settings': '店内WiFi'
  }
  return titleMap[route.path] || '首页'
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const logout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '退出登录', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    // 清除本地存储
    localStorage.removeItem('token')
    localStorage.removeItem('username')
    
    ElMessage.success('退出登录成功')
    router.push('/login')
  } catch (error) {
    // 用户取消退出
  }
}

onMounted(() => {
  // 从本地存储获取用户名
  const storedUsername = localStorage.getItem('username')
  if (storedUsername) {
    username.value = storedUsername
  }
})
</script>

<style scoped>
.dashboard-container {
  height: 100vh;
  background-color: #f5f7fa;
}

.el-container {
  height: 100%;
}

/* 侧边栏样式 */
.sidebar {
  background-color: #304156;
  transition: width 0.3s;
  overflow: hidden;
}

.logo-section {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 20px;
  background-color: #2c3e50;
  color: white;
}

.logo {
  width: 32px;
  height: 32px;
  margin-right: 10px;
}

.logo-text {
  font-size: 16px;
  font-weight: 600;
  color: white;
  margin: 0;
}

.mini-logo {
  width: 32px;
  height: 32px;
  background-color: #409eff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 14px;
}

.sidebar-menu {
  background-color: #304156;
  border: none;
}

.sidebar-menu :deep(.el-menu-item) {
  color: #bfcbd9;
  background-color: #304156;
}

.sidebar-menu :deep(.el-menu-item:hover) {
  background-color: #263445;
  color: #409eff;
}

.sidebar-menu :deep(.el-menu-item.is-active) {
  background-color: #409eff !important;
  color: white;
}

.sidebar-menu :deep(.el-sub-menu__title) {
  color: #bfcbd9;
  background-color: #304156;
}

.sidebar-menu :deep(.el-sub-menu__title:hover) {
  background-color: #263445;
  color: #409eff;
}

.sidebar-menu :deep(.el-sub-menu .el-menu-item) {
  background-color: #1f2d3d;
  color: #bfcbd9;
}

.sidebar-menu :deep(.el-sub-menu .el-menu-item:hover) {
  background-color: #001528;
  color: #409eff;
}

.sidebar-menu :deep(.el-sub-menu .el-menu-item.is-active) {
  background-color: #409eff !important;
  color: white;
}

/* 头部样式 */
.header {
  background-color: white;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-btn {
  font-size: 18px;
  cursor: pointer;
  color: #606266;
  margin-right: 20px;
  transition: color 0.3s;
}

.collapse-btn:hover {
  color: #409eff;
}

.breadcrumb {
  font-size: 16px;
  color: #303133;
}

.current-page {
  font-weight: 500;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.user-avatar {
  font-size: 20px;
  color: #409eff;
  margin-right: 8px;
}

.username {
  color: #303133;
  font-size: 14px;
  margin-right: 8px;
}

.dropdown-icon {
  font-size: 12px;
  color: #909399;
}

/* 主内容区样式 */
.main-content {
  background-color: #f5f7fa;
  padding: 20px;
  overflow-y: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    z-index: 1000;
    height: 100vh;
  }
  
  .header {
    margin-left: 0;
  }
  
  .main-content {
    margin-left: 0;
  }
}
</style> 